package com.business.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import javax.servlet.Filter;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: TODO
 * @Author: mis_wu
 * @Date: 2022/8/13
 **/
//@Configuration
public class DruidConfig {

    /**
     * 单独使用druid时可用
     * 注入druid的一些自定义属性 properties
     * @return DataSource
     */
    //@ConfigurationProperties(prefix = "spring.datasource")
    //@Bean
//    public DataSource druidDataSource(){
//        return new DruidDataSource();
//    }

    /**
     * 后天监控
     * @return ServletRegistrationBean
     * 项目启动访问路径:localhost:port/druid
     * /druid/*   正确
     * /druid/**  则访问不到
     */
    //@Bean
    public ServletRegistrationBean<StatViewServlet> statViewServlet(){
        ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        Map<String, String> initParameters = new HashMap<>();
        //登录的用户名和密码是固定的字段
        initParameters.put("loginUsername","admin");
        initParameters.put("loginPassword","123456");

        //允许访问用户
        initParameters.put("allow","");

        //禁止访问的用户
        initParameters.put("name","value:ip");

        registrationBean.setInitParameters(initParameters);
        return registrationBean;
    }

    /**
     * filter
     * @return FilterRegistrationBean
     */
    //@Bean
    public FilterRegistrationBean<Filter> webStatFilter(){
        FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new WebStatFilter());
        Map<String, String> initParameters = new HashMap<>();

        //可以过滤的请求
        initParameters.put("exclusions","/druid/*,*.js,*.css");

        registrationBean.setInitParameters(initParameters);
        return registrationBean;
    }

}
